﻿package smooth.core.object.flickrImageComponent {
	import app.core.action.RotatableScalable;
	import app.core.object.ImageLoadedEvent;
	import app.core.object.ImageObject;
	import smooth.core.object.flickrComponent.FlickrImageObject;
	import smooth.core.object.flickrComponent.FlickrLoader;
	import caurina.transitions.Tweener;
	import smooth.core.object.flickrComponent.FlickrLocalizationObject;
	import smooth.core.object.flickrComponent.FlickrPhotoInfosLoadedEvent;
	import smooth.core.object.flickrComponent.FlickrPhotoLocationLoadedEvent;
	import smooth.core.object.flickrUserComponent.FlickrUserComponent;
	import smooth.core.object.interactiveDateComponent.InteractiveDateComponent;
	import smooth.core.object.interactiveLocalizationComponent.InteractiveLocalizationComponent;
	import smooth.core.object.menuButtonComponent.MenuButtonChangedStateEvent;
	import smooth.core.object.menuButtonComponent.MenuButtonComponent;
	
	import flash.events.*;
	import flash.system.LoaderContext;
	import flash.display.*;
	import flash.net.URLRequest;
		
	
	public class FlickrImageComponent extends ImageObject {
		private var flickrImageObject:FlickrImageObject;
		private var menuButton:MenuButtonComponent;
		private var menu:FlickrImageMenu;
		private var initialWidth:int;
		private var initialHeight:int;
		private var initialPosX:int;
		private var initialPosY:int;
		private var initialColor:int;
		private var initialScale:Number;
		private var mouseSelect:Boolean;
		private var thisTweenX:Boolean;
		private var thisSlideX:Boolean;
		private var thisScaleDown:Boolean;
		
		public function FlickrImageComponent(name:String, flickrImageObject:FlickrImageObject, posX:int, posY:int, scale:Number, mouseSelect:Boolean, thisTweenX:Boolean, thisSlideX:Boolean, thisScaleDown:Boolean):void {
			// Init des attributs de l'objet
			this.name = name;
			this.flickrImageObject = flickrImageObject;
			// START - 20081123 - LSA
			this.addChild(flickrImageObject);
			// END - 20081123 - LSA
			this.initialPosX = posX;
			this.initialPosY = posY;
			this.initialScale = scale;
			this.mouseSelect = mouseSelect;
			this.thisTweenX = thisTweenX;
			this.thisSlideX = thisSlideX;
			this.thisScaleDown = thisScaleDown;
			// Catch ImageLoaded event 
			this.addEventListener(ImageLoadedEvent.imageLoaded, doProcessImageLoaded);	
			// Appel du constructeur parent
			var url:String = FlickrLoader.getPhotoUrl(flickrImageObject);
			super(url, this.mouseSelect, this.thisTweenX, this.thisSlideX, this.thisScaleDown);
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();
			// Init des process du composant
			initProcess();
		}
		
		private function initGraphics():void {
			this.scaleX = initialScale;
			this.scaleY = initialScale;
			this.x = initialPosX;
			this.y = initialPosX;
			// START - 20081123 - LSA
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			// START - 20081123 - LSA
			this.addEventListener(TouchEvent.MOUSE_DOWN, this.touchDown);						
			this.addEventListener(TouchEvent.MOUSE_UP, this.touchUp);
			// END - 20081123 - LSA
		}
		
		// START - 20081123 - LSA
		private function doProcessImageLoaded(e:ImageLoadedEvent):void {
			this.flickrImageObject.loadPhotoInfos();
			this.flickrImageObject.loadPhotoLocation();
			// Add menu component
			//this.menu = new FlickrImageMenu(0, 0, 0x0000FF, 140);
			this.menu = new FlickrImageMenu(0, 0, 0xFFFFFF, 140);
			this.flickrImageObject.addEventListener("flickrPhotoInfosLoaded", doProcessPhotoInfosLoaded, true, 0, true);
			this.flickrImageObject.addEventListener("flickrPhotoLocationLoaded", doProcessPhotoLocationLoaded, true, 0, true);
			this.menu.visible = false;
			this.addChild(this.menu);
			// Add menu button component
			//this.menuButton = new MenuButtonComponent(this, 0, 0, 120, 120, 0x0000FF, 0.5);
			this.menuButton = new MenuButtonComponent(this, 0, 0, 120, 120, 0xFFFFFF, 0.5);
			this.addChild(this.menuButton);
			this.menuButton.addEventListener(MenuButtonChangedStateEvent.menuButtonChangedState, this.showHideMenu);
		}
		
		private function touchDown(e:TouchEvent):void {
			this.menuButton.alpha = this.menuButton.initialAlpha;
		}
		
		private function touchUp(e:TouchEvent):void {
			if (this.menu.visible) {
				this.menuButton.alpha = this.menuButton.initialAlpha;
			} else {				
				this.menuButton.alpha = 0;
			}
		}
		
		private function showHideMenu(e:MenuButtonChangedStateEvent):void {
			if (e.show) {
				this.menu.alpha = 0;
				this.menu.visible = true;
				Tweener.addTween(this.menu, { alpha:1, time:1.5, transition:"easeOutBack" } );
			} else {
				this.menu.visible = false;
			}
		}
		
		private function doProcessPhotoInfosLoaded(e:FlickrPhotoInfosLoadedEvent):void {
			this.menu.addButton(new FlickrUserComponent(this.flickrImageObject.getOwner()));
			var pattern:RegExp = /-/g;
			var date:Date = new Date(this.flickrImageObject.getImageMetaData().getTakenDate().replace(pattern, "/"));
			this.menu.addButton(new InteractiveDateComponent(date, ""));
			this.flickrImageObject.removeEventListener("flickrPhotoInfosLoaded", doProcessPhotoInfosLoaded);
		}

		private function doProcessPhotoLocationLoaded(e:FlickrPhotoLocationLoadedEvent):void {
			if (this.flickrImageObject.getLocalization() != null) {
				var lat:Number = this.flickrImageObject.getLocalization().getLat();
				var lng:Number = this.flickrImageObject.getLocalization().getLng();
				var placeName:String = this.flickrImageObject.getLocalization().getNeighbourhood() + 
										" ("+this.flickrImageObject.getLocalization().getLocality()+")";
				this.menu.addButton(new InteractiveLocalizationComponent(lat, lng, placeName));
			}
			this.flickrImageObject.removeEventListener("flickrPhotoLocationLoaded", doProcessPhotoLocationLoaded);
		}
		
		public function removeComponent():void {
			var parentObject1:DisplayObjectContainer = this.parent;
			parentObject1.removeChild(this);
		}		
		
		public function getOwner():FlickrUserComponent {
			return new FlickrUserComponent(this.flickrImageObject.getOwner());
		}
		
		public function getLocalization():InteractiveLocalizationComponent {
			var localization:FlickrLocalizationObject = this.flickrImageObject.getLocalization();
			if (this.flickrImageObject.getLocalization() != null) {
				return new InteractiveLocalizationComponent(localization.getLat(), localization.getLng(), "");
			} else {
				return null;
			}
		}
		
		public function getTakenDate():InteractiveDateComponent {
			var pattern:RegExp = /-/g;
			if (this.flickrImageObject.getImageMetaData() != null) {
				var date:Date = new Date(this.flickrImageObject.getImageMetaData().getTakenDate().replace(pattern, "/"));
				return new InteractiveDateComponent(date, "")
			} else {
				return null;
			}
		}
		// END - 20081123 - LSA
		
		public function doAnimationFadeIn(maxWidth:int, maxHeight:int ):void {
			Tweener.addTween(this, { x:(Math.random()*maxWidth),y:(Math.random()*maxHeight), time:3, transition:"easeInOutBack" } );	
		}
		
	}
	
}